<template>
	<view class="addProject">
		<view class="form">
			<view class="xianmumincheng">
				<label><text class="xinghao">*</text>项目名称</label>
				<input type="text" name="uname" id="uname" placeholder="请输入" v-model="title">
			</view>
			<view class="education">
				<label><text class="xinghao">*</text>项目类别</label>
				<picker class="pickers" @change="bindPickerChange" :value="index" :range="arr">
					<view class="uni-input viewInput">{{arr[index]}}</view>
				</picker>
			</view>

			<view class="price">
				<label><text class="xinghao">*</text>项目价格</label>
				<input type="text" name="price" id="price" placeholder="请输入" v-model="price">
			</view>

			<!-- 项目内容 -->
			<view class="contents">
				<label><text class="xinghaos">*</text>项目内容</label>
				<textarea id="clinicProfile" v-model="content" placeholder="请输入"></textarea>
			</view>


			<view class="clinicmaingraph">
				<view><text class="xinghaoz">*</text> <text class="hong">项目主图(一张)</text> </view>

				<!-- 	<view><text class="xinghaos">*</text>项目主图</view>
				<input type="text" name="userName" id="userName" @click="clinicmaingraph" placeholder="上传 +" disabled="true">
				<view v-if="clinicmaingraphImg" class="images">
					<image :src="clinicmaingraphImg" mode="scaleToFill" style="width: 100rpx; height: 100rpx;"></image>
				</view> -->

				<view class="imagess">
					<image v-for="(item,index) in clinicmaingraphImg" :key="item.id" :src="item" mode="scaleToFill" style="width: 150rpx; height: 150rpx;"></image>
					<view @click="clinicmaingraph">
						<text class="jiahao">+</text>
					</view>
				</view>
			</view>


			<view class="clinicmaingraph dibujuli">
				<view><text class="xinghaoz">*</text> <text class="hong">项目详情(最多四张)</text> </view>
				<!-- <input type="text" name="userName" id="userName" @click="picturedetails" placeholder="上传 +" disabled="true"> -->
				<!-- <view v-if="picturedetailsImg" class="images">
					<image v-for="(item,index) in picturedetailsImg" :key="item.id" :src="item" mode="scaleToFill" style="width: 100rpx; height: 100rpx;"></image>
				</view> -->
				<view class="imagess">
					<image v-for="(item,index) in picturedetailsImg" :key="item.id" :src="item" mode="scaleToFill" style="width: 150rpx; height: 150rpx;"></image>
					<view @click="picturedetails">
						<text class="jiahao">+</text>
					</view>
				</view>

			</view>

		</view>

		<view class="addProjectanniu" @click="addProject">
			<text>添加</text>
		</view>

	</view>
</template>

<script>
	export default {
		onLoad() {
			// 获取oos上传
			this.uploadOosMethos();
			this.getIcon();
		},
		data() {
			return {
				title: "",
				price: "",
				content: "",
				image_url: null,
				clinicmaingraphImg: null,
				picturedetailsImg: null,
				// 下拉框的内容选择
				iconArr: null,
				arr: [],
				index: 0,
				img_url: [],
				
				// 上传的图片设置
				host: null,
				OSSAccessKeyId: null,
				dir: null,
				policy: null,
				signature: null,
			};
		},
		methods: {
			// 获取oos的地址
			async uploadOosMethos() {
				let fileGetUrl = await this.$myrequest({
					url: `File/get`
				});
				this.host = fileGetUrl.data.data.host;
				this.OSSAccessKeyId = fileGetUrl.data.data.accessid;
				this.dir = fileGetUrl.data.data.dir;
				this.policy = fileGetUrl.data.data.policy;
				this.signature = fileGetUrl.data.data.signature;
			},
			
			async picturedetails() {
				let self = this;
				uni.chooseImage({
					count: 4,
					success: res => {
						this.picturedetailsImg = res.tempFilePaths;
						
						for (let [keys, value] of Object.entries(res.tempFilePaths)) {
							
							let fileName = `${Date.now()+1}${value.substr(-4)}`;
							let key = `${self.dir}${fileName}`;
							
							self.img_url.push(`${self.host}/${self.dir}${fileName}`);
							
							uni.uploadFile({
								url: self.host,
								filePath: value,
								name: 'file',
								formData: {
									key,
									policy: self.policy,
									OSSAccessKeyId: this.OSSAccessKeyId,
									signature: this.signature
								},
								success: res => {
									// self.img_url.push(JSON.parse(uploadFileRes.data).msg);
									console.log(`详情图片`)
								}
							});

						};
					},
				});
			},
			// --详情图片--

			async getIcon() {
				let res = await this.$myrequest({
					url: `Pagelist/getIcon`,
				});
				this.iconArr = res.data.data;
				console.log(`$thiicondssd`)
				console.log(this.iconArr);
				console.log(`$thiicondssd`)
				for (let [key, value] of Object.entries(res.data.data)) {
					this.arr.push(value.icon_title);
				};
			},
			bindPickerChange(e) {
				this.index = e.target.value;
				console.log(this.index);

			},
			async addProject() {
				if (this.title === "" || this.title === null) {
					uni.showToast({
						title: "项目名称必填",
						icon: "none"
					})
					return false;
				};
				if (this.price === "" || this.price === null) {
					uni.showToast({
						title: "项目价格必填",
						icon: "none"
					})
					return false;
				};
				if (isNaN(this.price)) {
					uni.showToast({
						title: "价格必须为数字",
						icon: "none"
					})
					return false;
				};
				if (this.content === "" || this.content === null) {
					uni.showToast({
						title: "项目内容必填",
						icon: "none"
					})
					return false;
				};
				if (this.clinicmaingraphImg === "" || this.clinicmaingraphImg === null) {
					uni.showToast({
						title: "项目主图必填",
						icon: "none"
					})
					return false;
				};
				if (this.picturedetailsImg === null) {
					uni.showToast({
						title: "项目详情图必填",
						icon: "none"
					})
					return false;
				};
				let datas = {
					openid: this.$store.state.userinfo.openid,
					title: this.title,
					price: this.price,
					content: this.content,
					image_url: this.image_url,
					num_url: this.img_url.toString(),
					category_id: this.iconArr[this.index].id
				};
				console.log(`haodfngfigne `)
				console.log(datas);
				console.log(`haodfngfigne `)
				let timeOut = 5;
				uni.showToast({
					title: "图片上传中",
					icon: "none",
					duration: timeOut * 1000
				})
				let setId = setInterval(() => {
					timeOut = --timeOut;
					if (timeOut == 0) {
						this.addProjectImgs(datas);
						clearInterval(setId);
					};
				}, 1 * 1000);

			},
			async addProjectImgs(datas) {
				let res = await this.$myrequest({
					url: "Mianpage/addProject",
					method: "POST",
					data: datas
				});
				if (res.data.code == 200) {
					uni.redirectTo({
						url: `../projectManagement1/projectManagement1`
					});
				};
			},

			async clinicmaingraph() {
				let self = this;
				uni.chooseImage({
					count: 1,
					success: res => {
						this.clinicmaingraphImg = res.tempFilePaths;
						
						let fileName = `${Date.now()}${res.tempFilePaths[0].substr(-4)}`;
						let key = `${self.dir}${fileName}`;
						
						self.image_url = `${self.host}/${self.dir}${fileName}`;
						
						uni.uploadFile({
							url: self.host,
							filePath: res.tempFilePaths[0],
							name: 'file',
							formData: {
								key,
								policy: self.policy,
								OSSAccessKeyId: this.OSSAccessKeyId,
								signature: this.signature
							},
							success: (uploadFileRes) => {
								// self.image_url = JSON.parse(uploadFileRes.data).msg;
								console.log("image_url:", self.image_url)
							}
						});

					}
				});
			},

		}
	}
</script>

<style lang="scss">
	page {
		width: 100%;
		height: 100%;
	}

	.addProject {
		width: 100%;
		height: 100%;
		background-color: #FFFFFF;
		padding: 0 30rpx;
	}

	.form {
		>view {
			margin-top: 16rpx;
		}
	}

	.xinghao {
		position: absolute;
		top: 20rpx;
		left: 20rpx;
		color: #FF0000;
	}

	.xinghaoz {
		position: absolute;
		top: 20rpx;
		left: 20rpx;
		color: #FF0000;
	}

	.xianmumincheng,
	.price {
		height: 100rpx;
		@include chuizhilianduan;
		background-color: #F7F8FC;
		border-radius: 8rpx;
		position: relative;
		padding: 0 40rpx;

		>input {
			text-align: right;
			color: #B8B9BB;
		}
	}

	// 学历
	.education {
		height: 100rpx;
		@include chuizhilianduan;
		background-color: #F7F8FC;
		border-radius: 8rpx;
		position: relative;
		padding: 0 40rpx;

		.pickers {
			background-color: #F7F8FC;

			.viewInput {
				background-color: #F7F8FC;
				padding: 0;
				margin: 0;
				color: #B8B9BB;
			}
		}
	}

	// 内容
	.contents {
		display: flex;
		background-color: #F7F8FC;
		border-radius: 8rpx;
		position: relative;
		padding: 0 40rpx;
		flex-wrap: wrap;

		.xinghaos {
			position: absolute;
			top: -8rpx;
			left: -18rpx;
			color: #FF0000;
		}

		>label {
			position: relative;
			display: inline-block;
			width: 100%;
			margin: 20rpx 0;
		}

		#clinicProfile {
			height: 243rpx;
		}
	}


	// 添加图片
	.dibujuli {
		margin-bottom: 300rpx;
	}

	.clinicmaingraph {
		margin: 20rpx auto;
		border-radius: 10rpx;

		>view {
			padding-left: 20rpx;

			&:first-of-type {
				border-radius: 10rpx;
				height: 100rpx;
				background-color: rgba(247, 248, 252, 1);
				padding: 20rpx;
				@include chuizhilianduan;
				position: relative;
			}
		}

		.imagess {
			background-color: rgba(247, 248, 252, 1);
			border-radius: 0 0 10rpx 10rpx;
			padding-bottom: 30rpx;
			display: flex;

			image {
				margin-left: 20rpx;
			}

			view {
				display: inline-block;
				width: 150rpx;
				height: 150rpx;
				border: 2rpx dashed #C6C6CA;
				margin-left: 20rpx;
				text-align: center;
				line-height: 150rpx;

				.jiahao {
					font-size: 56rpx;
					color: #BBBCBD;
				}
			}
		}
	}

	// 添加按钮
	.addProjectanniu {
		@include dibuanniu;
		width: 689rpx;
	}

	.hong {
		margin-left: 20rpx !important;
	}
</style>
